package com.sptpc.demo.service.impl;

import com.sptpc.demo.mapper.CategoryMapper;
import com.sptpc.demo.mapper.DrinkMapper;
import com.sptpc.demo.model.Category;
import com.sptpc.demo.model.Drink;
import com.sptpc.demo.service.IDrinkService;
import com.sptpc.demo.vo.DrinkVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DrinkService implements IDrinkService {
    @Autowired
    private DrinkMapper drinkMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<Drink> getAllDrinks() {
        return drinkMapper.selectAll();
    }

    @Override
    public void addNewDrink(Drink drink, Long categoryId) {
        Category category = categoryMapper.selectByPrimaryKey(categoryId);
        drink.setCategoryId(categoryId);
        drinkMapper.insert(drink);
    }

    @Override
    public Drink getDrinkById(Long id) {
        return drinkMapper.selectByPrimaryKey(id.longValue());
    }

    @Override
    public void updateDrink(Drink drink) {
        drinkMapper.updateByPrimaryKeySelective(drink);
    }

    @Override
    public void deleteDrinkById(Long id) {
        drinkMapper.deleteByPrimaryKey(id.longValue());
    }

    @Override
    public List<DrinkVo> getAllDrinksWithCategory() {
        return drinkMapper.selectAllWithCategory();
    }
}
